package top.lyc829.zzl_android.ui.thing.dynamic_detail;

import android.util.Pair;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.DynamicBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.SonCommentBean;
import top.lyc829.zzl_android.logic.bean.SuperCommentBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class DynamicDetailViewModel extends ViewModel {

    private MutableLiveData<DynamicBean> dynamicBeanLiveData;
    private MutableLiveData<Integer> isPraiseLiveData;
    private MutableLiveData<Integer> isFellowLiveData;
    private MutableLiveData<List<SuperCommentBean>> superCommentsLiveData;
    private MutableLiveData<List<SonCommentBean>> sonCommentsLiveData;

    // first: 评论类型（1级 n级） second 评论id
    private MutableLiveData<Pair<Integer, Integer>> commentDeletePositionLiveData;


    //子评论数量变化时，通知一级评论刷新被评论的数量
    private MutableLiveData<Pair<Integer, Integer>> sonCommentChangePositionLiveData;


    private AtomicInteger loadingCount = new AtomicInteger(0);
    private MutableLiveData<Boolean> isCompleteLiveData;

    public MutableLiveData<Boolean> isCompleteLiveData() {
        if (isCompleteLiveData == null) {
            isCompleteLiveData = new MutableLiveData<>();
            isCompleteLiveData.postValue(false);
        }
        return isCompleteLiveData;
    }

    public AtomicInteger getLoadingCount() {
        return loadingCount;
    }

    public MutableLiveData<Pair<Integer, Integer>> getSonCommentChangePositionLiveData() {
        if (sonCommentChangePositionLiveData == null) {
            sonCommentChangePositionLiveData = new MutableLiveData<>();
        }
        return sonCommentChangePositionLiveData;
    }


    public MutableLiveData<Pair<Integer, Integer>> getCommentDeleteLiveData() {
        if (commentDeletePositionLiveData == null) {
            commentDeletePositionLiveData = new MutableLiveData<>();
            commentDeletePositionLiveData.setValue(new Pair<>(-1, -1));
        }
        return commentDeletePositionLiveData;
    }


    public MutableLiveData<List<SonCommentBean>> getSonCommentsLiveData() {
        if (sonCommentsLiveData == null) {
            sonCommentsLiveData = new MutableLiveData<>();
        }
        return sonCommentsLiveData;
    }

    public MutableLiveData<List<SuperCommentBean>> getSuperCommentsLiveData() {
        if (superCommentsLiveData == null) {
            superCommentsLiveData = new MutableLiveData<>();
        }
        return superCommentsLiveData;
    }

    public MutableLiveData<Integer> getIsFellowLiveData() {
        if (isFellowLiveData == null) {
            isFellowLiveData = new MutableLiveData<>();
        }
        return isFellowLiveData;
    }

    public MutableLiveData<Integer> getIsPraiseLiveData() {
        if (isPraiseLiveData == null) {
            isPraiseLiveData = new MutableLiveData<>();
        }
        return isPraiseLiveData;
    }

    public MutableLiveData<DynamicBean> getDynamicBeanLiveData() {
        if (dynamicBeanLiveData == null) {
            dynamicBeanLiveData = new MutableLiveData<>();
        }
        return dynamicBeanLiveData;
    }


    public void getDynamicById(int id) {
        Network.getDynamicById(id, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<DynamicBean> resultBean = JsonUtilNew.fromJsonObject(resultStr, DynamicBean.class);

                if (getLoadingCount().addAndGet(1) == 4) {
                    isCompleteLiveData().postValue(true);
                }

                if (resultBean.getCode().equals(200)) {
                    getDynamicBeanLiveData().postValue(resultBean.getData());

                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void getPraiseRelationShip(int userId, int type, int typeId) {
        Network.getPraiseRelationShip(userId, type, typeId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<Integer> resultBean = JsonUtilNew.fromJsonObject(resultStr, Integer.class);
                if (getLoadingCount().addAndGet(1) == 4) {
                    isCompleteLiveData().postValue(true);
                }
                if (resultBean.getCode().equals(200)) {
                    getIsPraiseLiveData().postValue(resultBean.getData());

                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    /**
     * 获取关注关系
     * @param userId 用户id
     * @param type 类型
     * @param typeId 目的id
     */
    public void getFellowRelationShip(int userId, int type, int typeId) {
        Network.getAttentionRelationship(userId, type, typeId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                TypeToken<ResultBean<Integer>> typeToken = new TypeToken<ResultBean<Integer>>() {};
                ResultBean<Integer> resultBean = new Gson().fromJson(resultStr, typeToken.getType());
                if (getLoadingCount().addAndGet(1) == 4) {
                    isCompleteLiveData().postValue(true);
                }
                if (resultBean.getCode().equals(200)) {
                    getIsFellowLiveData().postValue(resultBean.getData());
                } else if (resultBean.getCode().equals(201)) {
                    getIsFellowLiveData().postValue(0);
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                getIsFellowLiveData().postValue(0);
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void getSuperComments(int type, int typeId, int page) {
        Network.getSuperComments(typeId, page, type, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                ResultBean<List<SuperCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SuperCommentBean.class);
                if (page == 1) {
                    if (getLoadingCount().addAndGet(1) == 4) {
                        isCompleteLiveData().postValue(true);
                    }
                }
                if (resultBean.getCode().equals(200)) {
                    getSuperCommentsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }


    public void getSonComments(int commentId, int page) {
        Network.getSonComments(commentId, page, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                Utils.Sout(resultStr);
                ResultBean<List<SonCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, SonCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    getSonCommentsLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }



}
